<?php
/**
 * 接受采购系统推送的消息
 * @author sundongdong
 * @date 2016-07-25
 */
class Interface_PmsController extends Controller_Interface {

	CONST CHANGE_ORDER_QUENTITY_CLUMN = [
		'sku' => 'goods_sn',
		'stock_sn' => 'warehouse_code',
		'order_quantity' => 'stock_order_nums',
	];

	/**
	 * 从pms接受样衣处理数据
	 * @author sundongdong
	 */
	public function receiveSampleClothAction() {
		if (!$this->verifySign()) {
			exit('key error');
		}
		$data = $this->getRequest()->getParam('data');
		$data = @json_decode(stripslashes($data), true);
		if (!is_array($data)) {
			exit('empty data');
		}

		$wdb = Model_Db::getInstance('write_db');
		foreach ($data as $pms) {
			$sql = "update sample_cloth set sync_status=40,"
			. $wdb->quoteInto(" provide_qty = ?,", $pms['back_quantity'])
			. $wdb->quoteInto(' provide_status=?,', $pms['provide_status'])
			. $wdb->quoteInto(' provide_info=?', $pms['provide_info'])
			. " where sync_status=30"
			. $wdb->quoteInto(' and sku = ?', $pms['sku']);

			$wdb->query($sql);
		}
		// 成功返回
		exit('1');
	}

	/**
	 * 接口 从PMS被动接收订货数量
	 * Writer: QiuXiangCheng
	 * @return json
	 */
	public function getOrderGoodsAmountAction() {

		$str = '[{"sku":"100518103","order_quantity":"111","stock_sn":"FRFBAWH"},{"sku":"171462801","order_quantity":"55","stock_sn":"SZ"},{"sku":" \tFESFBE201609200001E-1","order_quantity":"110","stock_sn":"SZ"}]';
		set_time_limit(0);
		$key = Helper_Config::get('base', 'crypt', 'key');
		$get = $this->getRequest()->getParams();
		$result = ['status' => 0];
		if ($get['key'] != md5($key . $get['data'])) {
			$result['message'] = '签名验证失败..';
			$this->_helper->json($result);
		}
		$data = json_decode($get['data'], true);
		$inserted = $this->mappingUpdate($data, self::CHANGE_ORDER_QUENTITY_CLUMN, 'p_product_warehouse_stock', ['stock_sn', 'sku']);
		if ($inserted === true) {
			$result['status'] = 1;
			$result['message'] = '成功';
		}
		$this->_helper->json($result);
	}
	/**
	 * 从pms被动接受回货数量
	 * @author sundongdong
	 * @date 2016-08-22
	 */
	public function backGoodsAction() {
		set_time_limit(0);
		$result = ['status' => 0];
		if (!$this->verifySign()) {
			$result['message'] = '签名验证失败';
			$this->_helper->json($result);
		}
		$data = $this->getRequest()->getParam('data');
		$data = json_decode($data, true);
		if (!is_array($data) || !$data) {
			$result['message'] = '传送数据错误';
			$this->_helper->json($result);
		}
		$batch = [];
		foreach ($data as $s) {
			$temp = array();
			$temp['goods_sn'] = $s['sku'];
			$temp['stock_sn'] = $s['stock_sn'];
			$temp['plat_code'] = $s['plat_code'];
			$temp['back_quantity'] = $s['back_quantity'];
			$temp['date'] = date('Y-m-d');
			$batch[] = $temp;
		}
		$model = new Interface_Model_SpecialPurchase;
		try {
			$model->saveReturnGoods($batch);
			$result['status'] = 1;
			$result['message'] = '成功';
			$this->_helper->json($result);
		} catch (Exception $e) {
			$result['message'] = $e->getMessage();
			$this->_helper->json($result);
		}
	}
}